package logic

import (
	"context"
	"gorm.io/gorm"
	"strconv"
	"supply/models/mysql"

	"supply/supprpc/internal/svc"
	"supply/supprpc/supprpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
	return &UpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateLogic) Update(in *supprpc.UpdateRequest) (*supprpc.UpdateResponse, error) {
	// todo: add your logic here and delete this line

	var shop mysql.Shop
	mysql.DB.Debug().Where("id=?", in.Id).Find(&shop)

	m := mysql.Shop{
		Model: gorm.Model{ID: uint(in.Id)},
		Name:  in.Name,
		Price: float64(in.Price),
		Num:   in.Num,
	}
	mysql.DB.Debug().Updates(&m)
	return &supprpc.UpdateResponse{
		ShopId: strconv.Itoa(int(m.ID)),
	}, nil
}
